<template>
  <div class="goods-comment">
    <!-- 头部 -->
    <div class="head" v-if="commentInfo">
      <div class="data">
        <p>
          <span>{{ commentInfo.salesCount }}</span>
          <span>人购买</span>
        </p>
        <p>
          <span>{{ commentInfo.praisePercent }}</span>
          <span>好评率</span>
        </p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a
            v-for="item in commentInfo.tags"
            :key="item.title"
            href="javascript:;"
            @click="activeTitle = item.title"
            :class="{ active: activeTitle === item.title }"
          >
            {{ item.title }}（{{ item.tagCount }}）
          </a>
        </div>
      </div>
    </div>
    <!-- 排序 -->
    <div class="sort" v-if="commentInfo">
      <span>排序：</span>
      <a
        @click="changeSort(null)"
        href="javascript:;"
        :class="{ active: reqParams.sortField === null }"
      >
        默认
      </a>
      <a
        @click="changeSort('praiseCount')"
        href="javascript:;"
        :class="{ active: reqParams.sortField === 'praiseCount' }"
      >
        最热
      </a>
      <a
        @click="changeSort('createTime')"
        href="javascript:;"
        :class="{ active: reqParams.sortField === 'createTime' }"
      >
        最新
      </a>
    </div>
    <div class="list">
      <goods-comment-list :commentList="commentList" />
    </div>
    <XtxPagination
      :total="100"
      :pageSize="reqParams.pageSize"
      :currentPage="reqParams.page"
      @current-change="changePage"
    />
  </div>
</template>

<script>
import { findCommentInfoByGoods, findCommentListByGoods } from '@/api/goods'
import XtxPagination from '@/components/library/xtx-pagination.vue'
import { inject, reactive, ref, watch } from 'vue'
import goodsCommentList from './goods-comment-list.vue'
export default {
  components: { goodsCommentList, XtxPagination },
  name: 'GoodsComment',
  setup() {
    const goods = inject('goods')
    // 头部评价信息
    const commentInfo = ref(null)
    findCommentInfoByGoods(goods.value.id).then(({ result }) => {
      // 处理 tags 数组，方便模板 v-for 遍历
      result.tags = [
        { title: '全部评价', tagCount: result.evaluateCount },
        { title: '有图', tagCount: result.hasPictureCount },
        ...result.tags,
      ]
      commentInfo.value = result
    })

    // 筛选条件准备
    const reqParams = reactive({
      page: 1,
      pageSize: 10,
      hasPicture: null,
      tag: null,
      sortField: null,
    })

    // 改变排序
    const changeSort = (type) => {
      reqParams.sortField = type
      reqParams.page = 1
    }

    // 初始化或者筛选条件改变后，获取列表数据。
    const commentList = ref([])
    const total = ref(0)
    watch(
      reqParams,
      async () => {
        const data = await findCommentListByGoods(goods.value.id, reqParams)
        commentList.value = data.result.items
        total.value
      },
      { immediate: true },
    )

    // 记录当前激活的标签标题
    const activeTitle = ref('全部评价')

    // 分页切换
    const changePage = (page) => {
      reqParams.page = page
    }

    return {
      commentInfo,
      activeTitle,
      changeSort,
      commentList,
      reqParams,
      total,
      changePage,
    }
  },
}
</script>

<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }
          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
}
</style>
